home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTNEXT.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  109 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btnext.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btnext - next btree key
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btnext(btp)
  25.      btree_t *btp;
  26.  
  27. DESCRIPTION
  28.      The btnext function advances the cursor of btree btp to the next
  29.      key.  If cursor is currently null, it will be advanced to the
  30.      first key.  If the cursor is currently on the last key, it will
  31.      advance to null.  If the tree is empty, the cursor will remain on
  32.      null.
  33.  
  34.      btnext will fail if one or more of the following is true:
  35.  
  36.      [EINVAL]       btp is not a valid btree pointer.
  37.      [BTELOCK]      btp is not locked.
  38.      [BTENOPEN]     btp is not open.
  39.  
  40. SEE ALSO
  41.      btcursor, btfirst, btlast, btprev.
  42.  
  43. DIAGNOSTICS
  44.      Upon successful completion, a value of 0 is returned.  Otherwise,
  45.      a value of -1 is returned, and errno set to indicate the error.
  46.  
  47. ------------------------------------------------------------------------------*/
  48. #ifdef AC_PROTO
  49. int btnext(btree_t *btp)
  50. #else
  51. int btnext(btp)
  52. btree_t *btp;
  53. #endif
  54. {
  55.     int terrno = 0;        /* tmp errno */
  56.  
  57.     /* validate arguments */
  58.     if (!bt_valid(btp)) {
  59.         errno = EINVAL;
  60.         return -1;
  61.     }
  62.  
  63.     /* check if not open */
  64.     if (!(btp->flags & BTOPEN)) {
  65.         errno = BTENOPEN;
  66.         return -1;
  67.     }
  68.  
  69.     /* check locks */
  70.     if (!(btp->flags & BTLOCKS)) {
  71.         errno = BTELOCK;
  72.         return -1;
  73.     }
  74.  
  75.     /* advance cursor to next key in current node */
  76.     if (btp->cbtpos.node != NIL) {
  77.         if (++btp->cbtpos.key <= btp->cbtnp->n) {
  78.             return 0;
  79.         }
  80.     }
  81.  
  82.     /* advance cursor to null */
  83.     if (btp->cbtpos.node == btp->bthdr.last) {
  84.         btp->cbtpos.node = NIL;
  85.         btp->cbtpos.key = 0;
  86.         bt_ndinit(btp, btp->cbtnp);
  87.         return 0;
  88.     }
  89.  
  90.     /* advance cursor to first key in next node */
  91.     if (btp->cbtpos.node == NIL) {
  92.         btp->cbtpos.node = btp->bthdr.first;
  93.     } else {
  94.         btp->cbtpos.node = btp->cbtnp->rsib;
  95.     }
  96.     if (bt_ndget(btp, btp->cbtpos.node, btp->cbtnp) == -1) {
  97.         BTEPRINT;
  98.         terrno = errno;
  99.         btp->cbtpos.node = NIL;
  100.         btp->cbtpos.key = 0;
  101.         bt_ndinit(btp, btp->cbtnp);
  102.         errno = terrno;
  103.         return -1;
  104.     }
  105.     btp->cbtpos.key = 1;
  106.  
  107.     return 0;
  108. }
  109.